import Vue from 'vue'
import VueRouter from 'vue-router'
// import Elementui from 'element-ui'
// import HomeView from '../views/HomeView.vue'

// 导入路由(登录)组件
import Login from '../components/Login.vue'
import home from '../components/Home.vue'

// 导入用户列表组件
import Users from '../components/user/Users.vue'

// 导入权限列表组件
import Rights from '../components/power/Rights.vue'
// 导入角色列表组件
import Roles from '../components/power/Roles.vue'

// 导入商品分类组件
import Cate from '../components/goods/Cate'
// 导入商品参数组件
import Params from '../components/goods/Params.vue'
// 导入商品列表组件
import GoodsList from '../components/goods/List.vue'

// 导入订单组件
import Order from '../components/order/Order.vue'
// 导入报表组件
import Report from '../components/report/Report'

Vue.use(VueRouter)

const routes = [ //中括号
    // 路由匹配的组件

    // 路由重定向
    { path: '/', redirect: '/Login' },
    // 登录组件路由
    { path: '/login', component: Login },
    // 主页路由
    {
        path: '/home',
        component: home,
        // 这样只会显示一个
        // children: [{ path: '/users', component: Users }],
        // children: [{ path: '/rights', component: Rights }],
        // children: [{ path: '/roles', component: Roles }],
        // 这样才能多个显示
        children: [
            { path: '/users', component: Users },
            { path: '/rights', component: Rights },
            { path: '/roles', component: Roles },
            { path: '/categories', component: Cate },
            { path: '/params', component: Params },
            { path: '/goods', component: GoodsList },
            { path: '/orders', component: Order },
            { path: '/reports', component: Report },
        ],
    }
]
const router = new VueRouter({
    routes
})

// 路由守卫导航(控制页面访问权限)
router.beforeEach((to, from, next) => {
    // to 将要   from 来自    next 下一步操作
    // 用户访问登录页面，直接放行
    if (to.path == '/login') {
        return next();
    }
    //从sessionStorage 获取保存的token
    const token = window.sessionStorage.getItem('token')
        // 没有token强制跳转登录页面
    if (!token) {
        return next('/login');
    }
    next();
})





export default router